#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QImage>
#include <QPixmap>
#include <QFileDialog>
#include <opencv2/opencv.hpp>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->labelError->clear();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButtonSelectImage_clicked()
{
    QString imagePath = QFileDialog::getOpenFileName(this, tr("Open Image"), "", tr("Image Files (*.png *.jpg *.bmp *.tiff)"));
    ui->lineEditImagePath->setText(imagePath);
}

void MainWindow::on_pushButtonRun_clicked()
{
    // 清除错误信息
    ui->labelError->clear();

    // 读取图像路径
    QString imagePath = ui->lineEditImagePath->text();
    originalImage = cv::imread(imagePath.toStdString());

    if (originalImage.empty()) {
        ui->labelError->setText("Error: Invalid image path!");
        return;
    }

    // 显示原始图像
    cv::Mat displayOriginal;
    cv::cvtColor(originalImage, displayOriginal, cv::COLOR_BGR2RGB);
    QImage qImageOriginal(displayOriginal.data, displayOriginal.cols, displayOriginal.rows, displayOriginal.step, QImage::Format_RGB888);
    QPixmap pixmapOriginal = QPixmap::fromImage(qImageOriginal);
    ui->labelOriginal->setPixmap(pixmapOriginal);

    // 根据选择的图像类型处理图像
    cv::Mat processedImage = originalImage.clone();
    if (ui->checkBoxGray->isChecked()) {
        cv::cvtColor(processedImage, processedImage, cv::COLOR_BGR2GRAY);
        cv::cvtColor(processedImage, processedImage, cv::COLOR_GRAY2BGR); // 转回 BGR 格式以便显示
    }

    // 应用滤波器
    if (ui->checkBoxMean->isChecked()) {
        int kernelSize = ui->lineEditMeanKernel->text().toInt();
        if (kernelSize % 2 == 0 || kernelSize < 3) {
            ui->labelError->setText("Error: Mean filter kernel size must be an odd number greater than 2!");
            return;
        }
        cv::blur(processedImage, processedImage, cv::Size(kernelSize, kernelSize));
    }
    if (ui->checkBoxGaussian->isChecked()) {
        int kernelSize = ui->lineEditGaussianKernel->text().toInt();
        double sigma = ui->lineEditGaussianSigma->text().toDouble();
        if (kernelSize % 2 == 0 || kernelSize < 3) {
            ui->labelError->setText("Error: Gaussian filter kernel size must be an odd number greater than 2!");
            return;
        }
        if (sigma <= 0.0 || sigma > 10.0) {
            ui->labelError->setText("Error: Gaussian filter sigma must be between 0.1 and 10.0!");
            return;
        }
        cv::GaussianBlur(processedImage, processedImage, cv::Size(kernelSize, kernelSize), sigma);
    }
    if (ui->checkBoxMedian->isChecked()) {
        int kernelSize = ui->lineEditMedianKernel->text().toInt();
        if (kernelSize % 2 == 0 || kernelSize < 3) {
            ui->labelError->setText("Error: Median filter kernel size must be an odd number greater than 2!");
            return;
        }
        cv::medianBlur(processedImage, processedImage, kernelSize);
    }

    // 显示滤波后的图像
    cv::Mat displayFiltered;
    cv::cvtColor(processedImage, displayFiltered, cv::COLOR_BGR2RGB);
    QImage qImageFiltered(displayFiltered.data, displayFiltered.cols, displayFiltered.rows, displayFiltered.step, QImage::Format_RGB888);
    QPixmap pixmapFiltered = QPixmap::fromImage(qImageFiltered);
    ui->labelFiltered->setPixmap(pixmapFiltered);
}
